﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YY.UI
{
    public partial class CodeRuleFrm : Form
    {
        /// <summary>
        /// 编码默认长度
        /// </summary>
        const int DefualtCodeLen = 3;
        private string _inHeader = string.Empty;
        /// <summary>
        /// 编码输入前缀
        /// </summary>
        public string inHeader { get; set; }

        /// <summary>
        /// 获取或设置编码规则窗体的设定规则
        /// </summary>
        public CodeRule Rule { get; set; }

        public class CodeRule
        {
            public CodeRule()
            {
                this.autoCode = true;
                this.addParent = false;
                this.autoFill = false;
                this.header = string.Empty;
                this.len = DefualtCodeLen;
            }
            /// <summary>
            /// 是否自动编码
            /// </summary>
            public bool autoCode { get; set; }
            /// <summary>
            /// 编码流水长度
            /// </summary>
            public int len { get; set; }
            /// <summary>
            /// 编码前缀
            /// </summary>
            public string header { get; set; }
            /// <summary>
            /// 是否加上级编码段
            /// </summary>
            public bool addParent { get; set; }
            /// <summary>
            /// 流水是否自动补号
            /// </summary>
            public bool autoFill { get; set; }
        }

        public CodeRuleFrm(string title)
        {
            InitializeComponent();
            this.Text = title;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择流水长度");
                return;
            }
            string header = this.textBox1.Text.Trim();
            if (this.checkBox2.Checked)//加父级编码
                header += this.inHeader;
            this.Rule.autoCode = this.checkBox3.Checked;
            this.Rule.autoFill = this.checkBox1.Checked;
            this.Rule.addParent = this.checkBox2.Checked;
            this.Rule.header = header;
            this.Rule.len = Convert.ToInt16(this.comboBox1.SelectedItem);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bindList()
        {
            this.comboBox1.Items.Clear();
            for (int i = 1; i < 10; i++)
                this.comboBox1.Items.Add(i);
        }

        private void CodeRuleFrm_Load(object sender, EventArgs e)
        {
            bindList();
            if (this.Rule != null)
            {
                this.checkBox3.Checked = this.Rule.autoCode;
                this.checkBox1.Checked = this.Rule.autoFill;
                this.checkBox2.Checked = this.Rule.addParent;
                this.textBox1.Text = this.Rule.header;
                this.comboBox1.SelectedItem = this.Rule.len;
            }
            else
                this.comboBox1.SelectedItem = DefualtCodeLen;
        }

    }
}
